package com.framework.applogic;

import com.framework.model.User;
import com.framework.pages.UserProfilePage;

public class UserHelper extends DriverBasedHelper {

    public UserHelper(ApplicationManager app) {
        super(app.getWebDriver());
    }

    //Methods
    public void loginAs(User user) {
        pages.loginPage.ensurePageLoaded()
                .setUsernameField(user.getLogin())
                .setPasswordField(user.getPassword())
                .clickSubmitButton();
    }

    public void logout() {
        pages.internalPage.ensurePageLoaded()
                .clickLogoutLink();
    }

    public boolean isLoggedIn() {
        return pages.internalPage.waitInternalPageLoaded();
    }

    public boolean isLoggedInAs(User user) {
        return isLoggedIn() && getLoggedUser().getLogin().equals(user.getLogin());
    }

    public boolean isNotLoggedIn() {
        return pages.loginPage.isLoaded();
    }

    public void createUser(User user) {
        pages.internalPage.clickUserManagementLink().ensurePageLoaded()
                .setUsernameField(user.getLogin())
                .setEmailField(user.getEmail())
                .setPassword(user.getPassword())
                .setPassword2(user.getPassword())
                .clickSubmitButton();
    }

    private User getLoggedUser() {
        UserProfilePage userProfilePage = pages.internalPage.ensurePageLoaded()
                .clickUserProfilePage()
                .ensurePageLoaded();
        return new User()
                .setLogin(userProfilePage.getUsername())
                .setEmail(userProfilePage.getEmail())
                .setRole(userProfilePage.getRole());
    }

}







//    public boolean isNotLoggedIn() {
//        return pages.loginPage.waitPageLoaded();
//    }